package org.example.thread.testSequence;

/**
 * @author liyishan
 * @date 2024/10/25 13:52
 * @apiNote
 */

public class TestTreeSequence {

    public static void main(String[] args) {
        Thread t1 = new Thread(new Seq(0));
        Thread t2 = new Thread(new Seq(1));
        Thread t3 = new Thread(new Seq(2));
        t1.start();
        t2.start();
        t3.start();
    }
    private static volatile int count = 0;
    private static final Object lock = new Object();


    static class Seq implements Runnable{
        private final int index;
        public Seq(int index) {
            this.index = index;
        }


        @Override
        public void run() {
            while(count<100) {
                synchronized (lock) {
                    try{
                        while(count % 3 != index){
                            lock.wait();
                        }
                        System.out.println("Thread " + index + ": " + count);
                        count++;
                        lock.notifyAll();
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                }
            }
        }
    }
}
